package luj.game.robot.internal.concurrent.instance.command.service.factory;

import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableMap;
import luj.bean.api.BeanContext;
import luj.cluster.api.actor.Tellable;
import luj.game.robot.api.action.CommandService;
import luj.game.robot.internal.concurrent.instance.command.map.CommandMap;

import java.util.Map;

import static com.google.common.base.Preconditions.checkNotNull;

public class CommandServiceFactory {

  public CommandServiceFactory(Tellable instanceRef, Map<String, Object> fromParam, String fromCmd, BeanContext lujbean, CommandMap commandMap, Class<?> cmdType) {
    _instanceRef = instanceRef;
    _fromParam = fromParam;
    _fromCmd = fromCmd;
    _lujbean = lujbean;
    _commandMap = commandMap;
    _cmdType = cmdType;
  }

  public <P> CommandService<P> create() {
    String cmdName = _cmdType.getName();
    CommandMap.Command cmd = _commandMap.get(cmdName);
    checkNotNull(cmd, cmdName);

    var service = new ServiceImpl<P>();
    service._cmd = cmd;
    service._fromParam = MoreObjects.firstNonNull(_fromParam, ImmutableMap.of());
    service._factory = this;

    return service;
  }

  final Tellable _instanceRef;

  final String _fromCmd;
  final BeanContext _lujbean;

  private final CommandMap _commandMap;
  private final Class<?> _cmdType;

  private final Map<String, Object> _fromParam;
}
